package com.detu.ivos.dictoption.controller;

import com.detu.ivos.base.response.JsonResult;
import com.detu.ivos.dict.service.DictService;
import com.detu.ivos.dictoption.pojo.dto.DictOptionQuery;
import com.detu.ivos.dictoption.pojo.dto.DictOptionSaveParam;
import com.detu.ivos.dictoption.pojo.vo.DictOptionVO;
import com.detu.ivos.dictoption.service.DictOptionService;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/dictoption")
@Slf4j
public class DictOptionController {
    @Autowired
    DictOptionService dictOptionService;

    @GetMapping("select")
    public JsonResult select(DictOptionQuery dictOptionQuery){
        log.debug("查询的字典Id{}",dictOptionQuery);
        List<DictOptionVO> dictOptionVOS= dictOptionService.select(dictOptionQuery);
        log.debug("查到的结果是={}",dictOptionVOS);
        return JsonResult.ok(dictOptionVOS);
    }
    @PostMapping("save")
    public JsonResult save(DictOptionSaveParam dictOptionSaveParam){
        log.debug("添加的字典={}",dictOptionSaveParam);
        dictOptionService.insertDictOption(dictOptionSaveParam);
        return JsonResult.ok();
    }
    @PostMapping("delete")
    public JsonResult deleteById(DictOptionSaveParam dictOptionSaveParam){
        log.debug("删除字典项的id={}",dictOptionSaveParam.getId());
        dictOptionService.deleteById(dictOptionSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select/{code}")
    public JsonResult selectDictOptionByCode(@PathVariable String code){
        log.debug("根据字典code查询字典项：code={}",code);
        List<DictOptionVO> list=
                dictOptionService.selectDictOptionByCode(code);
        return JsonResult.ok(list);
    }
}
